<template>
  <div class="home-container">
    <div class="welcome-section">
      <el-skeleton :loading="loading" animated>
        <div class="welcome-content">
          <div class="user-info">
            <div class="avatar-container">
              <el-avatar :src="avatar" :size="50" class="user-avatar">
                <img src="@/assets/imgs/avatar.gif" alt="" />
              </el-avatar>
              <div class="avatar-badge">
                <Icon icon="ep:check" />
              </div>
            </div>
            <div class="user-text">
              <div class="greeting-text">{{ getGreeting() }}</div>
              <div class="welcome-text">
                {{ t('workplace.welcome') }} <span class="username">{{ username }}</span>
                {{ t('workplace.happyDay') }}
              </div>
              <div class="weather-info">
                <Icon icon="ep:sunny" class="weather-icon" />
                <span>{{ t('workplace.toady') }}，20℃ - 32℃</span>
                <span class="weather-desc">晴朗</span>
              </div>
            </div>
          </div>
          <div class="stats-info">
            <div class="stat-item">
              <div class="stat-icon">
                <Icon icon="ep:folder" />
              </div>
              <div class="stat-content">
                <div class="stat-label">{{ t('workplace.project') }}</div>
                <CountTo
                  class="stat-value"
                  :start-val="0"
                  :end-val="totalSate.project"
                  :duration="2000"
                />
              </div>
            </div>
            <div class="stat-item">
              <div class="stat-icon">
                <Icon icon="ep:list" />
              </div>
              <div class="stat-content">
                <div class="stat-label">{{ t('workplace.toDo') }}</div>
                <CountTo
                  class="stat-value"
                  :start-val="0"
                  :end-val="totalSate.todo"
                  :duration="2000"
                />
              </div>
            </div>
            <div class="stat-item">
              <div class="stat-icon">
                <Icon icon="ep:view" />
              </div>
              <div class="stat-content">
                <div class="stat-label">{{ t('workplace.access') }}</div>
                <CountTo
                  class="stat-value"
                  :start-val="0"
                  :end-val="totalSate.access"
                  :duration="2000"
                />
              </div>
            </div>
          </div>
        </div>
      </el-skeleton>
    </div>

    <!-- 第一行：快捷入口 + 图表区域 -->
    <div class="top-row">
      <div class="shortcut-section">
        <div class="section-header">
          <h3>{{ t('workplace.shortcutOperation') }}</h3>
        </div>
        <el-skeleton :loading="loading" animated>
          <div class="shortcut-grid">
            <div
              v-for="item in shortcut"
              :key="`team-${item.name}`"
              class="shortcut-item"
              @click="handleShortcutClick(item.url)"
            >
              <Icon :icon="item.icon" :style="{ color: item.color }" class="shortcut-icon" />
              <span>{{ item.name }}</span>
            </div>
          </div>
        </el-skeleton>
      </div>

      <div class="charts-section">
        <div class="chart-item">
          <Echart :options="pieOptionsData" :height="'100%'" />
        </div>
        <div class="chart-item">
          <Echart :options="barOptionsData" :height="'100%'" />
        </div>
      </div>
    </div>

    <!-- 第二行：项目数模块 -->
    <div class="project-section">
      <div class="section-header">
        <h3>{{ t('workplace.project') }}</h3>
        <el-link
          type="primary"
          :underline="false"
          href="https://github.com/yudaocode"
          target="_blank"
        >
          {{ t('action.more') }}
        </el-link>
      </div>
      <el-skeleton :loading="loading" animated>
        <div class="project-grid">
          <div
            v-for="(item, index) in projects"
            :key="`card-${index}`"
            class="project-card"
            @click="handleProjectClick(item.message)"
          >
            <div class="project-header">
              <Icon :icon="item.icon" :size="20" :style="{ color: item.color }" />
              <span class="project-name">{{ item.name }}</span>
            </div>
            <div class="project-desc">{{ t(item.message) }}</div>
            <div class="project-footer">
              <span class="project-type">{{ item.personal }}</span>
              <span class="project-date">{{ formatTime(item.time, 'yyyy-MM-dd') }}</span>
            </div>
          </div>
        </div>
      </el-skeleton>
    </div>

    <!-- 右侧：通知区域 -->
    <div class="right-section">
      <div class="notice-section">
        <div class="section-header">
          <h3>{{ t('workplace.notice') }}</h3>
          <el-link type="primary" :underline="false">{{ t('action.more') }}</el-link>
        </div>
        <el-skeleton :loading="loading" animated>
          <div class="notice-list">
            <div v-for="(item, index) in notice" :key="`dynamics-${index}`" class="notice-item">
              <div class="notice-header">
                <div class="notice-type">{{ item.type }}</div>
                <div class="notice-date">{{ formatTime(item.date, 'MM-dd') }}</div>
              </div>
              <div class="notice-title">
                <Highlight :keys="item.keys.map((v) => t(v))">
                  {{ item.title }}
                </Highlight>
              </div>
            </div>
          </div>
        </el-skeleton>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { set } from 'lodash-es'
import { EChartsOption } from 'echarts'
import { formatTime } from '@/utils'

import { useUserStore } from '@/store/modules/user'
// import { useWatermark } from '@/hooks/web/useWatermark'
import type { WorkplaceTotal, Project, Notice, Shortcut } from './types'
import { pieOptions, barOptions } from './echarts-data'
import { useRouter } from 'vue-router'

defineOptions({ name: 'Index' })

const { t } = useI18n()
const router = useRouter()
const userStore = useUserStore()
// const { setWatermark } = useWatermark()
const loading = ref(true)
const avatar = userStore.getUser.avatar
const username = userStore.getUser.nickname
const pieOptionsData = reactive<EChartsOption>(pieOptions) as EChartsOption
// 获取统计数
let totalSate = reactive<WorkplaceTotal>({
  project: 0,
  access: 0,
  todo: 0
})

const getCount = async () => {
  const data = {
    project: 40,
    access: 2340,
    todo: 10
  }
  totalSate = Object.assign(totalSate, data)
}

// 获取项目数
let projects = reactive<Project[]>([])
const getProject = async () => {
  const data = [
    {
      name: 'ruoyi-vue-pro',
      icon: 'simple-icons:springboot',
      message: 'github.com/YunaiV/ruoyi-vue-pro',
      personal: 'Spring Boot 单体架构',
      time: new Date('2025-01-02'),
      color: '#6DB33F'
    },
    {
      name: 'yudao-ui-admin-vue3',
      icon: 'ep:element-plus',
      message: 'github.com/yudaocode/yudao-ui-admin-vue3',
      personal: 'Vue3 + element-plus 管理后台',
      time: new Date('2025-02-03'),
      color: '#409EFF'
    },
    {
      name: 'yudao-ui-mall-uniapp',
      icon: 'icon-park-outline:mall-bag',
      message: 'github.com/yudaocode/yudao-ui-mall-uniapp',
      personal: 'Vue3 + uniapp 商城手机端',
      time: new Date('2025-03-04'),
      color: '#ff4d4f'
    },
    {
      name: 'yudao-cloud',
      icon: 'material-symbols:cloud-outline',
      message: 'github.com/YunaiV/yudao-cloud',
      personal: 'Spring Cloud 微服务架构',
      time: new Date('2025-04-05'),
      color: '#1890ff'
    },
    {
      name: 'yudao-ui-admin-vben',
      icon: 'devicon:antdesign',
      message: 'github.com/yudaocode/yudao-ui-admin-vben',
      personal: 'Vue3 + vben5(antd) 管理后台',
      time: new Date('2025-05-06'),
      color: '#e18525'
    },
    {
      name: 'yudao-ui-admin-uniapp',
      icon: 'ant-design:mobile',
      message: 'github.com/yudaocode/yudao-ui-admin-uniapp',
      personal: 'Vue3 + uniapp 管理手机端',
      time: new Date('2025-06-01'),
      color: '#2979ff'
    }
  ]
  projects = Object.assign(projects, data)
}

// 获取通知公告
let notice = reactive<Notice[]>([])
const getNotice = async () => {
  const data = [
    {
      title: '系统支持 JDK 8/17/21，Vue 2/3',
      type: '技术兼容性',
      keys: ['JDK', 'Vue'],
      date: new Date()
    },
    {
      title: '后端提供 Spring Boot 2.7/3.2 + Cloud 双架构',
      type: '架构灵活性',
      keys: ['Boot', 'Cloud'],
      date: new Date()
    },
    {
      title: '全部开源，个人与企业可 100% 直接使用，无需授权',
      type: '开源免授权',
      keys: ['无需授权'],
      date: new Date()
    },
    {
      title: '国内使用最广泛的快速开发平台，远超 10w+ 企业使用',
      type: '广泛企业认可',
      keys: ['最广泛', '10w+'],
      date: new Date()
    }
  ]
  notice = Object.assign(notice, data)
}

// 获取快捷入口
let shortcut = reactive<Shortcut[]>([])

const getShortcut = async () => {
  const data = [
    {
      name: '首页',
      icon: 'ion:home-outline',
      url: '/',
      color: '#1fdaca'
    },
    {
      name: '商城中心',
      icon: 'ep:shop',
      url: '/mall/home',
      color: '#ff6b6b'
    },
    {
      name: 'AI 大模型',
      icon: 'tabler:ai',
      url: '/ai/chat',
      color: '#7c3aed'
    },
    {
      name: 'ERP 系统',
      icon: 'simple-icons:erpnext',
      url: '/erp/home',
      color: '#3fb27f'
    },
    {
      name: 'CRM 系统',
      icon: 'simple-icons:civicrm',
      url: '/crm/backlog',
      color: '#4daf1bc9'
    },
    {
      name: 'IoT 物联网',
      icon: 'fa-solid:hdd',
      url: '/iot/home',
      color: '#1a73e8'
    }
  ]
  shortcut = Object.assign(shortcut, data)
}

// 用户来源
const getUserAccessSource = async () => {
  const data = [
    { value: 335, name: 'analysis.directAccess' },
    { value: 310, name: 'analysis.mailMarketing' },
    { value: 234, name: 'analysis.allianceAdvertising' },
    { value: 135, name: 'analysis.videoAdvertising' },
    { value: 1548, name: 'analysis.searchEngines' }
  ]
  set(
    pieOptionsData,
    'legend.data',
    data.map((v) => t(v.name))
  )
  pieOptionsData!.series![0].data = data.map((v) => {
    return {
      name: t(v.name),
      value: v.value
    }
  })
}
const barOptionsData = reactive<EChartsOption>(barOptions) as EChartsOption

// 周活跃量
const getWeeklyUserActivity = async () => {
  const data = [
    { value: 13253, name: 'analysis.monday' },
    { value: 34235, name: 'analysis.tuesday' },
    { value: 26321, name: 'analysis.wednesday' },
    { value: 12340, name: 'analysis.thursday' },
    { value: 24643, name: 'analysis.friday' },
    { value: 1322, name: 'analysis.saturday' },
    { value: 1324, name: 'analysis.sunday' }
  ]
  set(
    barOptionsData,
    'xAxis.data',
    data.map((v) => t(v.name))
  )
  set(barOptionsData, 'series', [
    {
      name: t('analysis.activeQuantity'),
      data: data.map((v) => v.value),
      type: 'bar'
    }
  ])
}

// 获取时间问候语
const getGreeting = () => {
  const hour = new Date().getHours()
  if (hour < 6) return '夜深了'
  if (hour < 9) return '早上好'
  if (hour < 12) return '上午好'
  if (hour < 14) return '中午好'
  if (hour < 17) return '下午好'
  if (hour < 19) return '傍晚好'
  if (hour < 22) return '晚上好'
  return '夜深了'
}

const getAllApi = async () => {
  await Promise.all([
    getCount(),
    getProject(),
    getNotice(),
    getShortcut(),
    getUserAccessSource(),
    getWeeklyUserActivity()
  ])
  loading.value = false
}

const handleProjectClick = (message: string) => {
  window.open(`https://${message}`, '_blank')
}

const handleShortcutClick = (url: string) => {
  router.push(url)
}

getAllApi()
</script>

<style lang="scss" scoped>
.home-container {
  min-height: 100vh;
}

.welcome-section {
  position: relative;
  padding: 16px 20px;
  margin-bottom: 16px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgb(0 0 0 / 10%);
}

.welcome-content {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar-container {
  position: relative;
}

.user-avatar {
  border: 3px solid rgb(255 255 255 / 30%);
  box-shadow: 0 4px 12px rgb(0 0 0 / 15%);
}

.avatar-badge {
  position: absolute;
  right: 2px;
  bottom: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  font-size: 12px;
  color: #fff;
  background: #52c41a;
  border: 2px solid #fff;
  border-radius: 50%;
}

.user-text {
  .greeting-text {
    margin-bottom: 2px;
    font-size: 13px;
    font-weight: 500;
    color: #333;
  }

  .welcome-text {
    margin-bottom: 4px;
    font-size: 16px;
    font-weight: 600;
    color: #333;

    .username {
      font-weight: 700;
      color: #ffd700;
    }
  }

  .weather-info {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: #333;

    .weather-icon {
      font-size: 16px;
      color: #ffd700;
    }

    .weather-desc {
      padding: 2px 8px;
      margin-left: 4px;
      font-size: 11px;
      background: #f5f5f5;
      border-radius: 4px;
    }
  }
}

.stats-info {
  display: flex;
  gap: 24px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 4px;
  transition: all 0.3s ease;

  &:hover {
    background: #e9ecef;
    transform: translateY(-2px);
  }
}

.stat-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  font-size: 18px;
  color: #333;
  background: #f0f0f0;
  border-radius: 10px;
}

.stat-content {
  .stat-label {
    margin-bottom: 2px;
    font-size: 11px;
    font-weight: 500;
    color: #333;
  }

  .stat-value {
    font-size: 20px;
    font-weight: 700;
    color: #333;
  }
}

.top-row {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 16px;
  min-height: 320px;
  margin-bottom: 16px;
  align-items: start;
}

.project-section {
  padding: 20px;
  margin-bottom: 16px;
  background: #fff;
  border: 1px solid #e8e8e8;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 8px;
  margin-bottom: 16px;
  border-bottom: 1px solid #f0f0f0;

  h3 {
    margin: 0;
    font-size: 14px;
    font-weight: 500;
    color: #262626;
  }
}

.project-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.project-card {
  position: relative;
  padding: 20px;
  cursor: pointer;
  background: #fff;
  border: 1px solid #f0f0f0;
  border-radius: 6px;
  box-shadow: 0 1px 3px rgb(0 0 0 / 4%);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

  &::before {
    position: absolute;
    background: linear-gradient(135deg, rgb(255 255 255 / 8%), rgb(255 255 255 / 4%));
    border-radius: 12px;
    content: '';
    opacity: 0;
    transition: opacity 0.3s ease;
    inset: 0;
  }

  &:hover {
    background: #fff;
    border-color: #d9d9d9;
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgb(0 0 0 / 8%);

    &::before {
      opacity: 1;
    }
  }

  &:active {
    transform: translateY(0);
    transition-duration: 0.1s;
  }
}

.project-header {
  position: relative;
  z-index: 1;
  display: flex;
  margin-bottom: 12px;
  align-items: center;
  gap: 12px;

  .project-name {
    font-size: 15px;
    font-weight: 600;
    letter-spacing: -0.01em;
    color: #1a1a1a;
  }
}

.project-desc {
  position: relative;
  z-index: 1;
  display: -webkit-box;
  margin-bottom: 16px;
  overflow: hidden;
  font-size: 13px;
  line-height: 1.5;
  color: #666;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.project-footer {
  position: relative;
  z-index: 1;
  display: flex;
  font-size: 12px;
  color: #999;
  justify-content: space-between;
  align-items: center;

  .project-type {
    padding: 4px 8px;
    font-weight: 500;
    color: #666;
    background: #f5f5f5;
    border-radius: 6px;
  }

  .project-date {
    font-size: 11px;
    color: #bbb;
  }
}

.charts-section {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  height: 100%;
  min-height: 320px;
}

.chart-item {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 320px;
  padding: 16px;
  background: #fff;
  border: 1px solid #e8e8e8;
}

.right-section {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 16px;
}

.shortcut-section {
  height: 100%;
  min-height: 320px;
  padding: 20px;
  background: #fff;
  border: 1px solid #e8e8e8;
}

.notice-section {
  padding: 20px;
  background: #fff;
  border: 1px solid #e8e8e8;
}

.shortcut-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.shortcut-item {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 16px 12px;
  overflow: hidden;
  cursor: pointer;
  background: #fafafa;
  border: 1px solid #f0f0f0;
  border-radius: 6px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

  &::before {
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, rgb(255 255 255 / 10%), rgb(255 255 255 / 5%));
    content: '';
    opacity: 0;
    transition: opacity 0.3s ease;
  }

  &:hover {
    background: #fff;
    border-color: #d9d9d9;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgb(0 0 0 / 10%);

    &::before {
      opacity: 1;
    }

    .shortcut-icon {
      transform: scale(1.1);
    }
  }

  &:active {
    transform: translateY(0);
    box-shadow: 0 2px 6px rgb(0 0 0 / 10%);
  }

  span {
    font-size: 12px;
    font-weight: 500;
    line-height: 1.2;
    color: #666;
    text-align: center;
  }
}

.notice-list {
  max-height: 300px;
  overflow-y: auto;
}

.notice-item {
  padding: 12px 0;
  cursor: pointer;
  border-bottom: 1px solid #f0f0f0;
  transition: background-color 0.2s ease;

  &:last-child {
    border-bottom: none;
  }

  &:hover {
    background-color: #fafafa;
  }

  &:active {
    background-color: #f0f0f0;
  }
}

.notice-header {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;

  .notice-type {
    padding: 4px 8px;
    font-size: 11px;
    font-weight: 500;
    color: #666;
    background: #f5f5f5;
    border-radius: 4px;
  }

  .notice-date {
    font-size: 11px;
    color: #999;
  }
}

.notice-title {
  position: relative;
  z-index: 1;
  display: -webkit-box;
  overflow: hidden;
  font-size: 13px;
  line-height: 1.5;
  color: #333;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

@media (width <= 1200px) {
  .main-content {
    grid-template-columns: 1fr;
  }

  .project-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }

  .project-card {
    padding: 16px;
  }

  .charts-section {
    grid-template-columns: 1fr;
  }
}

@media (width <= 1024px) {
  .shortcut-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }

  .shortcut-item {
    padding: 14px 10px;
  }

  .shortcut-icon {
    font-size: 22px;
  }
}

@media (width <= 768px) {
  .home-container {
    padding: 12px;
  }

  .welcome-content {
    flex-direction: column;
    gap: 16px;
  }

  .user-info {
    flex-direction: column;
    text-align: center;
    gap: 8px;
  }

  .stats-info {
    gap: 12px;
    flex-wrap: wrap;
    justify-content: center;
  }

  .stat-item {
    flex: 1;
    min-width: 100px;
    padding: 10px 12px;
  }

  .project-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .project-card {
    padding: 16px;
  }

  .shortcut-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .notice-item {
    padding: 8px 0;
  }

  .shortcut-item {
    padding: 12px 8px;
  }

  .shortcut-icon {
    font-size: 20px;
  }

  .shortcut-item span {
    font-size: 11px;
  }
}
</style>
